package com.xsky.logic.company;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.basic.DateConverter;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.xsky.converter.XStreamClobConverter;
import com.xsky.database.dao.impl.BaseEntityDaoImpl;
import com.xsky.operation.vo.TabCompany;

public class CompanyLogic
{
	public String add(String xmlStr)//增加报送信息
	{
		String dataXml="";
		try
		{
			Document document = DocumentHelper.parseText(xmlStr);
			dataXml = document.selectSingleNode("//data").asXML();
		} catch (DocumentException e)
		{
			e.printStackTrace();
		}
		BaseEntityDaoImpl baseDao = new BaseEntityDaoImpl();
		XStream xs = new XStream(new DomDriver());
		String defaultFormat = "yyyy-MM-dd HH:mm:ss";
		String[] acceptableFormat = {"yyyy-MM-dd HH:mm:ss","yyyy-MM-dd","yyyy-MM"};
		xs.registerConverter(new DateConverter(defaultFormat,acceptableFormat));
		xs.registerConverter(new XStreamClobConverter());
		xs.alias("data", TabCompany.class);
		TabCompany entity = (TabCompany)xs.fromXML(dataXml);
		entity.setCreatetime(new Date());
		entity.setId(UUID.randomUUID().toString().substring(0,31));
		baseDao.insertEntity(entity);
		return "<data><result>ok</result><uuid>"+entity.getId()+"</uuid><info>添加成功</info></data>";
	}
	public String delete(String xmlStr)//删除
	{
		String dataXml="";
		String in = "(";
		try
		{
			Document document = DocumentHelper.parseText(xmlStr);
			List itemList = document.selectNodes("//root/item");
			Element element = null;
			for(int i = 0; i < itemList.size(); i++)
			{
				element = (Element)itemList.get(i);
				document = DocumentHelper.parseText(element.asXML());
				if(element.attribute("selected")==null)
				{
					
				}
				else
				{
					if("true".equals(element.attribute("selected").getData().toString()))
					{
						in+="'"+document.selectSingleNode("//item/id").getText()+"',";
					}
				}
			}
		} catch (DocumentException e)
		{
			e.printStackTrace();
		}
		in += ")";
		in = in.replace(",)", ")");
		BaseEntityDaoImpl baseDao = new BaseEntityDaoImpl();
		baseDao.deleteByHql("delete TabCompany model where model.id in "+in);
		return "<data><result>ok</result><uuid></uuid><info>删除成功</info></data>";
	}
	public String deleteById(String id)//删除
	{
		BaseEntityDaoImpl baseDao = new BaseEntityDaoImpl();
		baseDao.deleteByHql("delete TabCompany model where model.id='"+id+"'");
		return "<data><result>ok</result><uuid></uuid><info>删除成功</info></data>";
	}
	public String update(String xmlStr)//更新报送信息
	{
		String dataXml="";
		try
		{
			Document document = DocumentHelper.parseText(xmlStr);
			dataXml = document.selectSingleNode("//data").asXML();
		} catch (DocumentException e)
		{
			e.printStackTrace();
		}
		BaseEntityDaoImpl baseDao = new BaseEntityDaoImpl();
		XStream xs = new XStream(new DomDriver());
		String defaultFormat = "yyyy-MM-dd HH:mm:ss";
		String[] acceptableFormat = {"yyyy-MM-dd HH:mm:ss","yyyy-MM-dd","yyyy-MM"};
		xs.registerConverter(new DateConverter(defaultFormat,acceptableFormat));
		xs.registerConverter(new XStreamClobConverter());
		xs.alias("data", TabCompany.class);
		TabCompany entity = (TabCompany)xs.fromXML(dataXml);
		entity.setCreatetime(new Date());
		baseDao.updateEntity(entity);
		return "<data><result>ok</result><uuid></uuid><info>更新成功</info></data>";
	}
	public String genDetail(String id)
	{
		BaseEntityDaoImpl baseDao = new BaseEntityDaoImpl();
		TabCompany entity = new TabCompany();
		entity = (TabCompany)baseDao.findByPrimaryKey(entity, "id", id);
		XStream xs = new XStream(new DomDriver());
		xs.alias("data", TabCompany.class);
		String xmlStr = xs.toXML(entity);
		return xmlStr;
	}
	public String genAll()
	{
		BaseEntityDaoImpl baseDao = new BaseEntityDaoImpl();
		List list = baseDao.findAll(TabCompany.class.getName());
		String result = "<data>";
		XStream xs = new XStream(new DomDriver());
		String defaultFormat = "yyyy-MM";
		String[] acceptableFormat = {"yyyy-MM"};
		xs.registerConverter(new DateConverter(defaultFormat,acceptableFormat));
		xs.registerConverter(new XStreamClobConverter());
		xs.alias("item", TabCompany.class);
		for (int i =0; i<list.size(); i++)
		{
			TabCompany entity = (TabCompany)list.get(i);
			result+=xs.toXML(entity);
		}
		result+="</data>";
		return result.replace("-01 00:00:00.0", "");
	}
	public String genCompanyWithCondition(String condition)
	{
		try
		{
			Document document = DocumentHelper.parseText(condition);
			String dataXml = document.selectSingleNode("//condition").asXML();
		} catch (DocumentException e)
		{
			e.printStackTrace();
		}
		BaseEntityDaoImpl baseDao = new BaseEntityDaoImpl();
		List list = baseDao.findAll(TabCompany.class.getName());
		String result = "<data>";
		XStream xs = new XStream(new DomDriver());
		String defaultFormat = "yyyy-MM";
		String[] acceptableFormat = {"yyyy-MM"};
		xs.registerConverter(new DateConverter(defaultFormat,acceptableFormat));
		xs.registerConverter(new XStreamClobConverter());
		xs.alias("item", TabCompany.class);
		for (int i =0; i<list.size(); i++)
		{
			TabCompany entity = (TabCompany)list.get(i);
			result+=xs.toXML(entity);
		}
		result+="</data>";
		return result.replace("-01 00:00:00.0", "");
	}
}
